package rubik;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;

public class CubeCanvas extends Canvas {
    private BufferedImage build;
    
    private final int dfs = 195; // default frame size
    
    private BufferedImage w,g,r,y,b,o = null;
    private final Point F = new Point(dfs,dfs);
    private final Point B = new Point(dfs*3,dfs);
    private final Point U = new Point(dfs,0);
    private final Point D = new Point(dfs,dfs*2);
    private final Point L = new Point(0,dfs);
    private final Point R = new Point(dfs*2,dfs);
    
    private CubeGraphics graph;
    private Cube cube;
    
    public CubeCanvas(CubeGraphics graph, Cube cube) {
        this.cube = cube;
        this.graph = graph;
        this.setSize(graph.getSize());
        this.setBackground(Color.BLACK);
        try {
            w = ImageIO.read(new File("img/white.png"));
            g = ImageIO.read(new File("img/green.png"));
            r = ImageIO.read(new File("img/red.png"));
            y = ImageIO.read(new File("img/yellow.png"));
            b = ImageIO.read(new File("img/blue.png"));
            o = ImageIO.read(new File("img/orange.png"));
        }
        catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "Immagini non trovate!", "ERRORE", JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }
    }
    
    @Override
    public void paint(Graphics g) {
        update(g);
    }
    
    @Override
    public void update(Graphics g) {
        build = new BufferedImage((int)this.getSize().getWidth(),(int)this.getSize().getHeight(),BufferedImage.TYPE_INT_ARGB);
        drawFace('u');
        drawFace('l');
        drawFace('f');
        drawFace('r');
        drawFace('b');
        drawFace('d');
        ((Graphics2D)g).drawImage(build,0,0,(int)this.getSize().getWidth(),(int)this.getSize().getHeight(),null);
    }
    
    private void drawFace(char f) {
        int startx = 0,starty = 0;
        switch(f) {
        case 'u': 
            startx = U.x;
            starty = U.y;
            break;
        case 'd': 
            startx = D.x;
            starty = D.y;
            break;
        case 'f': 
            startx = F.x;
            starty = F.y;
            break;
        case 'b': 
            startx = B.x;
            starty = B.y;
            break;
        case 'l': 
            startx = L.x;
            starty = L.y;
            break;
        case 'r': 
            startx = R.x;
            starty = R.y;
            break;
        }
        int step = dfs/cube.getDimension();
        for(int i=0; i<cube.getDimension(); i++) {
            for(int j=0; j<cube.getDimension(); j++) {
                int px = startx + (step * i);
                int py = starty + (step * j);
                switch (cube.getFace(f)[j][i]) {
                    case 'w': build.createGraphics().drawImage(w,px,py,step,step,null);
                              break;
                    case 'r': build.createGraphics().drawImage(r,px,py,step,step,null);
                              break;
                    case 'g': build.createGraphics().drawImage(g,px,py,step,step,null);
                              break;
                    case 'y': build.createGraphics().drawImage(y,px,py,step,step,null);
                              break;
                    case 'b': build.createGraphics().drawImage(b,px,py,step,step,null);
                              break;
                    case 'o': build.createGraphics().drawImage(o,px,py,step,step,null);
                              break;
                }
            }
        }
    }
    public void setCube(Cube cube) {
        this.cube = cube;
    }
}
